<template>
  <input type="text" placeholder="姓名" v-model="e.name">
  <input type="text" placeholder="工资" v-model="e.salary">
  <input type="text" placeholder="工作" v-model="e.job">
  <button @click="add()">添加</button>
  <table border="1">
    <caption>员工列表</caption>
    <tr>
      <th>编号</th><th>姓名</th><th>工资</th><th>工作</th><th>操作</th>
    </tr>
    <tr v-for="(emp,i) in arr">
      <td>{{i+1}}</td>
      <td>{{emp.name}}</td>
      <td>{{emp.salary}}</td>
      <td>{{emp.job}}</td>
      <td><button @click="del(emp,i)">删除</button></td>
    </tr>
  </table>
</template>

<script setup>
  import {ref} from "vue";
  const e = ref({name:"",salary:"",job:""});
  const add = ()=>{
    //创建一个新对象 数据来自e
    let newEmp = {...e.value};
    //把用户输入的员工信息 添加到数组中
    arr.value.push(newEmp);
    e.value = {name:"",salary:"",job:""};//清空文本框
  }
  const del = (emp,i)=>{
    if (confirm("您确认删除"+emp.name+"吗?")){
      arr.value.splice(i,1);
    }
  }
  const arr = ref([{name:"刘备",salary:3000,job:"销售"},
    {name:"关羽",salary:4000,job:"人事"},
    {name:"张飞",salary:5000,job:"程序员"}]);
</script>

<style scoped>

</style>